﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using Telerik.Web.UI;
using BSCMFF.Core.BusinessLayer;
using BSCMFF.Core;

namespace BSCMFF.Web.Pages.Evaluation_Setup
{
    public partial class EvaluateEmployees : MultiLanguagePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BSCEntities context = new BSCEntities();
            
            if (Request.QueryString["EvaluationEvaluatorEmployee"] != null)
            {
                int eeeID = Int32.Parse(Request.QueryString["EvaluationEvaluatorEmployee"]);
                Evaluation_Evaluator_Employee eee = (from ee in context.Evaluation_Evaluator_Employee
                                                     where ee.ID == eeeID
                                                     select ee).SingleOrDefault();

                EvalSelection.PublicSelectedValue = eee.Evaluation_FK; ;
            }

            
                if (Session["userID"] == null)
                {
                    UserManager um = new UserManager();
                    Session.Add("userID", (int)(um.GetUserByLogin(User.Identity.Name)).ID);
                }


                

                //List<User> nusers = (from eee in context.Evaluation_Evaluator_Employee
                //                    where eee.Evaluation_FK == 28 && eee.Filled == false
                //                    select eee.User).ToList();

                //List<User> nnusers = (from user in context.Users.Include("Evaluation_Evaluator_Employee")

                //                      where user.Evaluation_Evaluator_Employee.FirstOrDefault().Evaluation_FK == 28
                //                      select user).ToList();

                int currUserId = (int)Session["userID"];

                FillInterviewNeededGrid(EvalSelection.PublicSelectedValue, currUserId);
                FillNotEvaluatedEmployeesGrid(EvalSelection.PublicSelectedValue, currUserId);

                FillEvaluatedEmployeesGrid(EvalSelection.PublicSelectedValue, currUserId);

                  
        }

        private void FillInterviewNeededGrid(int evalID, int evaluatorID)
        {
            BSCEntities context = new BSCEntities();
            //List<User> nnnusers = (from eee in context.Evaluation_Evaluator_Employee
            //                       where eee.Evaluation_FK == evalID && eee.Filled == false && eee.Evaluator_FK == evaluatorID
            //                       select eee.User).ToList();

            List<Evaluation_Evaluator_Employee> eees = (from eee in context.Evaluation_Evaluator_Employee.Include("User.SBU")
                                           where eee.Evaluation_FK == evalID && eee.Filled == false && eee.Evaluator_FK == evaluatorID
                                           select eee).ToList();


            RadGridInterviewNeeded.DataSource = eees;// nnnusers;
            RadGridInterviewNeeded.DataBind();
        }

        private void FillNotEvaluatedEmployeesGrid(int evalID, int evaluatorID)
        {
            BSCEntities context = new BSCEntities();
            //List<User> nnnusers = (from eee in context.Evaluation_Evaluator_Employee
            //                       where eee.Evaluation_FK == evalID && 
            //                             eee.Filled == true && 
            //                             eee.Evaluated == false && 
            //                             eee.Evaluator_FK == evaluatorID
            //                       select eee.User).ToList();

            List<Evaluation_Evaluator_Employee> eees = (from eee in context.Evaluation_Evaluator_Employee.Include("User.SBU")
                                                        where eee.Evaluation_FK == evalID &&
                                                         eee.Filled == true &&
                                                         eee.Evaluated == false &&
                                                         eee.Evaluator_FK == evaluatorID
                                                        select eee).ToList();

            RadGridNotEvaluatedEmployees.DataSource = eees;// nnnusers;
            RadGridNotEvaluatedEmployees.DataBind();
        }

        private void FillEvaluatedEmployeesGrid(int evalID, int evaluatorID)
        {
            BSCEntities context = new BSCEntities();
            //List<User> users = (from eee in context.Evaluation_Evaluator_Employee
            //                    where eee.Evaluation_FK == evalID && eee.Evaluated == true && eee.Evaluator_FK == evaluatorID
            //                    select eee.User).ToList();

            List<Evaluation_Evaluator_Employee> eees = (from eee in context.Evaluation_Evaluator_Employee.Include("User.SBU")
                                                        where eee.Evaluation_FK == evalID && eee.Evaluated == true && eee.Evaluator_FK == evaluatorID
                                                        select eee).ToList();

            //foreach (User u in users)
            //{
            //    context.LoadProperty(u, "Evaluation_Evaluator_Employee");
            //}

            //RadGridEvaluatedEmployees = null;
            RadGridEvaluatedEmployees.DataSource = eees;// users;
            RadGridEvaluatedEmployees.DataBind();
        }

        protected void EvaluationSelectedIndexChanged(object sender, EventArgs e)
        {
            RadComboBoxSelectedIndexChangedEventArgs re = (RadComboBoxSelectedIndexChangedEventArgs)e;
            int evalID = Int32.Parse(re.Value);
            int currUserId = (int)Session["userID"];

            FillEvaluatedEmployeesGrid(evalID, currUserId);
            FillNotEvaluatedEmployeesGrid(evalID, currUserId);

            //BSCEntities context = new BSCEntities();



            //List<User> users = (from eu in context.Evaluation_Evaluator_Employee
            //                    where eu.Evaluation_FK == evalID
            //                    select eu.User).ToList();

            //foreach(User u in users)
            //{
            //    context.LoadProperty(u, "Evaluation_Evaluator_Employee");
            //}
            //RadGridEvaluatedEmployees.DataSource = users;
            //RadGridEvaluatedEmployees.DataBind();

            //User u = new User(); 
            //u.Evaluation_Evaluator_Employee1.First().Filled

            //Evaluation_Evaluator_Employee eee = new Evaluation_Evaluator_Employee();
            //eee.Filled;
        }

        protected void RadGridInterviewNeeded_ItemCommand(object sender, GridCommandEventArgs e)
        {
            int eeeid = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString());

            if (e.CommandName == "Delete")
            {
                EvaluationManager em = new EvaluationManager();
                //em.DeleteEvaluationEvaluatorEmployee(eeeid);
                //FillEvaluatedEmployeesGrid(
                //RadGridEvaluatedEmployees.Rebind();
                Response.Redirect(Request.Url.AbsolutePath);
            }
            else
            {
                Response.Redirect("EvaluationFillingMode.aspx?Step=Interview&Mode=" + e.CommandName + "&EvaluationEvaluatorEmployee=" + eeeid);
            }
        }

        protected void RadGridEvaluatedEmployees_ItemCommand(object sender, GridCommandEventArgs e)
        {
            int eeeid = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString());

            if (e.CommandName == "Delete")
            {
                EvaluationManager em = new EvaluationManager();
                em.DeleteEvaluationEvaluatorEmployee(eeeid);
                //FillEvaluatedEmployeesGrid(
                //RadGridEvaluatedEmployees.Rebind();
                Response.Redirect(Request.Url.AbsolutePath);
            }
            else
            {
                Response.Redirect("EvaluationFillingMode.aspx?Mode=" + e.CommandName + "&EvaluationEvaluatorEmployee=" + eeeid);
            }
            //switch (e.CommandName) 
            //{
            //    case "Select": Response.Redirect("EvaluationFillingMode.aspx?EvaluationEvaluatorEmployee=" + eeeid); break;
            //}
        }

        protected void RadGridNotEvaluatedEmployees_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                int eeeid = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString());
                Response.Redirect("EvaluationFillingMode.aspx?EvaluationEvaluatorEmployee=" + eeeid);
            }
        }

        protected void RadGridEvaluatedEmployees_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (Request.QueryString["EvaluationEvaluatorEmployee"] != null)
            {
                int eeeID = Convert.ToInt32(Request.QueryString["EvaluationEvaluatorEmployee"]);

                if (e.Item is GridDataItem)
                {
                    GridDataItem dataItem = e.Item as GridDataItem;
                    int fieldValue = int.Parse(dataItem["ID"].Text);
                    if (eeeID == fieldValue)
                    {
                        e.Item.Selected = true;
                        //RadGridEvaluatedEmployees.SelectedValue = eeeID;
                    }
                }
            }
        }

        
    }
}